/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.ws.client.program;

import java.lang.reflect.Method;

import net.bioteam.appweb.TestConfig;
import net.bioteam.appweb.user.User;
import net.bioteam.appweb.ws.WsClient;

public class ProgramTest
{

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception
	{
		TestConfig.UnixInit();
		User user=new User();
		user.setName(TestConfig.username);
		user.setPassword(TestConfig.password);
		WsClient client=new WsClient(user, TestConfig.serviceURL, null);
		
		BlastallTest btest=new BlastallTest();
		btest.client=client;
		runTests(btest, args);
		
		Emboss eTest=new Emboss();
		eTest.client=client;
		runTests(eTest, args);
		
		EmbassyMisc ey=new EmbassyMisc();
		ey.client=client;
		runTests(ey, args);
		
		EmbossHmmer em=new EmbossHmmer();
		em.client=client;
		runTests(em, args);
		
		EmbossPhylip ep=new EmbossPhylip();
		ep.client=client;
		runTests(ep, args);
		
		OtherProgramsTest op=new OtherProgramsTest();
		op.client=client;
		runTests(op, args);
		
		TestConfig.shutdown();
	}
	
	public static void runTests(Object obj, String args[])
	{
		String programName=null;
		if (args.length==1 || args.length==3)
		{
			programName="test"+args[0];
			programName.replace("-", "_");
		}
		if (args.length==3  && args[1].equals("-c"))
		{
			TestConfig.clusterid=args[2];
		}
		if (args.length==2  && args[0].equals("-c"))
		{
			TestConfig.clusterid=args[1];
		}
		Method[] mss=obj.getClass().getMethods();
		for (Method ms: mss)
		{
			if (programName!=null )
			{
				if (ms.getName().equalsIgnoreCase(programName))
				{
					try
					{
						TestConfig.logger.info(" invoke program "+ms.getName().substring(4));
						ms.invoke(obj);
					} catch (Exception e)
					{
						e.printStackTrace();
					} 
				}
			} else if (ms.getName().startsWith("test"))
			{
				try
				{
					TestConfig.logger.info(" invoke program "+ms.getName().substring(4));
					ms.invoke(obj);
				} catch (Exception e)
				{
					e.printStackTrace();
				}  
				
			}
		}
	}

}
